/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
var getKthFromEnd = function (head, k) {
  let stack = []
  let ans = null
  while (head) {
    stack.push(head.val)
    head = head.next
  }
  while (k != 0) {
    let tmp = new ListNode(stack.pop())
    tmp.next = ans
    ans = tmp
    k--
  }
};